/**
*文件管理：1、挂载文件；2、按条件获取文件
**/
Ext.define('WebOs.Kernel.ServiceFileSystem', {
	sfsstore : null,

	constructor : function(config) {
		var me = this;
		Ext.apply(me, config);
		me.sfsstore = new Ext.util.MixedCollection();
		me.init();
		return me;
	},

	mountFile : function(model) {
		var me = this;
		if(me.hasFile(model)){
			log('[WARN]','已存在相同的文件,name:'+model.get('name') + ',cls:'+model.get('serviceCls'));
			return null;
		}
		if (model)
			me.sfsstore.add(model.get('name'), model);
		return model.get('name');
	},
	
	hasFile : function(model){
		var me = this;
		
		var file = me.getOneFileByCondition(function(item,key){
			if(item.get('name')==model.get('name') || (item.get('serviceCls') && item.get('serviceCls')==model.get('serviceCls')))
				return true;
			return false;
		});
		if(file)
			return true;
		return false;
	},

	createFileModelFromJson : function(json, pname, level) {
		json.pname = pname;
		json.level = level;
		var fileModel = new WebOs.Kernel.model.FileModel(json);
		return fileModel;
	},

	mountFilesToParentFile : function(json, pname, level) {
		var me = this;
		if (!pname) {
			pname = Constants.SFS_ROOT_NAME;
		}
		var fmodel = me.createFileModelFromJson(json, pname, level);
		var fname = me.mountFile(fmodel);
		var children = json.children;
		if (children) {
			for (var p in children) {
				me.mountFilesToParentFile(children[p], fname, level);
			}
		}
	},

	getFilesByCondition : function(fn, scope) {
		var me = this;
		var cols = me.sfsstore.findBy(fn, scope || this);
		return cols;
	},
	/**
	 * fn : function(item,key){}
	 * **/
	getOneFileByCondition : function(fn, scope) {
		var me = this;
		var col = me.sfsstore.findBy(fn, scope || this);
		return col;
	},

	getFileByKey : function(name) {
		var me = this;
		return me.sfsstore.get(name);
	},

	init : function() {
		var me = this;
		var rootFile = new WebOs.Kernel.model.FileModel({
			pname : null,
			name : Constants.SFS_ROOT_NAME,
			text : Constants.SFS_ROOT_NAME,
			serviceType : Constants.SFS_TYPE_TOC
		});
		me.mountFile(rootFile);
	}
});